package MyQueue;

public class MycircularQueue {

    /*
    *https://leetcode.cn/problems/design-circular-queue/description/
    * 循环队列 设计循环队列
    *
    * */
    public int[] elem;

    public int front;
    public int rear;

    public MycircularQueue(int size) {
        this.elem=new int[size];
    }

    //入队
    public boolean enQueue(int val){
        if(isFUll()){
            return false;
        }
        this.elem[rear]=val;
        rear=(rear+1)%elem.length;
        return true;
    }
    //删除队头元素
    public boolean deQueue(){
        if(isEmpty()){
            return false;
        }
        front=(front+1)%elem.length;

        return true;
    }

    //得到队头不删除
    public int Front(){
        if(isEmpty()){
            return -1;
        }
        return this.elem[front];
    }
    //得到队尾元素不删除

    public int Rear(){
        if(isEmpty()){
            return -1;
        }

        int index=(rear==0)?  elem.length-1: rear-1;
        return elem[index];
      /*  if(rear==0){
            return elem.length-1;
        }
        return this.elem[rear-1];
        */
    }
    //判断队列满不满
    public boolean isFUll(){
        return (rear+1)%elem.length==front;
    }

    //队列为空
    public boolean isEmpty(){

        return front==rear;
    }
}
